package com.xxdh.service.impl;

import com.xxdh.domain.dto.LoginUserDto;
import com.xxdh.domain.po.User;
import com.xxdh.domain.po.UserExt;
import com.xxdh.mapper.UserMapper;
import com.xxdh.service.IUserExtService;
import com.xxdh.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxdh.util.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwg
 * @since 2024-10-15
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private final IUserExtService userExtService;

    /**
     * 登录最后处理，如：设置cookie等
     * @param userId
     * @param response
     */
    @Override
    public void finalLogin(String userId, HttpServletResponse response) {
        // 创建一个Cookie对象
        Cookie myCookie = new Cookie("userId", userId);
        // 设置Cookie的过期时间为1小时（3600秒）
        myCookie.setMaxAge(3600);
        // 设置Cookie的路径（可选）
        myCookie.setPath("/");
        // 将Cookie添加到响应中
        response.addCookie(myCookie);
        // 设置响应内容类型并发送响应（这里只是简单地返回一个消息）
        response.setContentType("application/json");
    }

    /**
     * 注册
     * @param loginUser 登录用户信息
     * @param response 响应
     * @return
     */
    @Override
    public String register(LoginUserDto loginUser, HttpServletResponse response) {
        User user = User.builder().userName(loginUser.getUserName())
                .userId(loginUser.getUserId())
                .type(loginUser.getType() == null || "0".equals(loginUser.getType()) ? "0" : "1")
                .build();
        boolean flag = save(user);
        if(!flag) {
            return Result.fail("创建用户失败").toJSONString();
        }
        UserExt userExt = UserExt.builder().userId(Long.valueOf(loginUser.getUserId())).password(loginUser.getPassword()).build();
        userExtService.save(userExt);
        return Result.ok().toJSONString();
    }

    /**
     * 根据学生id获取学生信息
     * @param studentId 学生id
     * @return
     */
    @Override
    public User getUserById(Long studentId) {
        return lambdaQuery().eq(User::getUserId, studentId).one();
    }

    /**
     * 修改密码
     * @param userId
     * @param newPassword
     */
    @Override
    public void updatePassword(Long userId, String newPassword) {
        userExtService.lambdaUpdate().eq(UserExt::getUserId, userId).set(UserExt::getPassword, newPassword).update();
    }

    @Override
    public void updateUser(Long userId, User user) {
        lambdaUpdate().eq(User::getUserId, userId)
                .set(user.getUserId() != null, User::getUserId, user.getUserId())
                .set(user.getUserName() != null, User::getUserName, user.getUserName())
                .set(user.getType() != null, User::getType, user.getType())
                .set(user.getSubject() != null, User::getSubject, user.getSubject())
                .set(user.getRoom() != null, User::getRoom, user.getRoom())
                .set(user.getIdNumber() != null, User::getIdNumber, user.getIdNumber())
                .set(user.getTeacherName() != null, User::getTeacherName, user.getTeacherName()).update();
    }

}
